package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.RoleModel;
import com.zhongyouyun.admin.model.SiteListModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.pojo.RolePojo;
import com.zhongyouyun.admin.pojo.SiteListPojo;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.IPUtils;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.SiteListService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2022-11-14
 * @Description:
 */
@RestController
@RequestMapping("/admin/z_site_list")
@Api(value = "首页应用管理controller", tags = {"首页应用管理操作接口"})
public class SiteListController {
    @Autowired
    private SiteListService service;

    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "获取角色信息", notes = "通过角色获取角色信息")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = RoleModel.class)
    })
    public IPage<SiteListPojo> findPage(@RequestBody PageRequest pageRequest, HttpServletRequest request) {

        QueryWrapper<RolePojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_number");
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
//        logger.error("===============","=================");
        return service.selectPojoPage(page, queryWrapper);
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody SiteListModel siteListModel) {
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(model.getId()) && siteListModel.getId() != 0) {
            siteListModel.setUpdateBy(model.getId().toString());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(siteListModel, model.getId().toString());
        }
        Boolean flag = service.saveOrUpdate(siteListModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<SiteListModel> siteListModelList) {

        List<Long> list = new ArrayList<Long>();
        siteListModelList.forEach(siteListModel -> {
            list.add(siteListModel.getId());
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

}
